package com.kgc.storeprovider.controller;

import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.InStore;
import com.kgc.store.pojo.OutStore;
import com.kgc.store.pojo.ProDivide;
import com.kgc.store.remote.ProDivideRemote;
import com.kgc.storeprovider.service.ProCombineService;
import com.kgc.storeprovider.service.ProDivideService;
import com.kgc.storeprovider.service.StoreGoodSummaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@CrossOrigin
public class ProDivideController implements ProDivideRemote {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ProDivideService proDivideService;
    @Autowired
    private ProCombineService proCombineService;
    @Autowired
    private StoreGoodSummaryService storeGoodSummaryService;

    @Override
    public ResponseMsg getOne(String code) {
        logger.info("ProDivideController getOne code:{}", code);
        ResponseMsg responseMsg = proDivideService.getByCode(code);
        logger.debug("proDivideService getOne responseMsg:{}", responseMsg+",code:"+code);
        return responseMsg;
    }

    @Override
    public ResponseMsg getPage(Map<String, Object> map) {
        logger.info("ProDivideController getPage map:{}", map);
        ResponseMsg responseMsg = proDivideService.getByPage(map);
        logger.debug("proDivideService getPage responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg add(ProDivide proDivide) {
        logger.info("ProDivideController add proCombine:{}", proDivide);
        ResponseMsg responseMsg = proDivideService.add(proDivide);
        logger.debug("proDivideService add responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg update(ProDivide proDivide) {
        logger.info("ProDivideController update proDivide:{}", proDivide);
        ResponseMsg responseMsg = proDivideService.update(proDivide);
        logger.debug("proDivideService update responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg del(String code) {
        logger.info("ProDivideController del proCombine:{}", code);
        ResponseMsg responseMsg = proDivideService.delete(code);
        logger.debug("proDivideService del responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg changStatus(String code, String status) {
        logger.info("ProDivideController changStatus code:{},status:{}", code, status);
        ResponseMsg responseMsg = proDivideService.changeStatus(code, status);
        logger.debug("ProDivideController changStatus responseMsg:{}", responseMsg+",code:"+code+",status:"+status);
        return responseMsg;
    }
    @Override
    public ResponseMsg changeStore( ProDivide proDivide) {
        logger.info("ProCombineController changeStore: {}", proDivide);
        ResponseMsg responseMsg = new ResponseMsg();
        // 1. 成品出库
        OutStore out = proDivideService.createOutStoreFromProCombine(proDivide);
        logger.debug("成品出库单: {}", out);

        // 2. 原料入库
        InStore in = proDivideService.createInStoreFromProCombine(proDivide);
        logger.debug("原料入库单: {}", in);

        // 3. 更新库存: 先出库成品，再入库原料
        Map<String,Object> map = new HashMap<>();
        // 成品出库
        map.put("stock", proDivide.getCount() * -1);
        map.put("gid", proDivide.getBom().getProduct().getId());
        map.put("sid", proDivide.getSids().get(0));
        storeGoodSummaryService.method(map);

        // 原料入库
        List<Good> materials = proDivide.getBom().getMaterial();
        for (int i = 0; i < materials.size(); i++) {
            map.clear();
            map.put("stock", proDivide.getCount());
            map.put("gid", materials.get(i).getId());
            map.put("sid", proDivide.getSids().get(i + 1));
            storeGoodSummaryService.method(map);
        }
        responseMsg.setCode("200");
        return responseMsg;
    }
}
